// ITI 1120 Fall 2006, Lab 11, Example 2

/**
 * This class represents a line in (x-y) coordinate space, and is stored as a
 * pair of (x,y) end points.
 * <p>
 * Operations on lines include setting the end points, determining the line
 * length, translating the line in x-y space, printing line information, and
 * determining the slope of the line.
 */

public class Line
{
 // CLASS VARIABLES:

 // (none)

 // INSTANCE VARIABLES:

 /**
  * Start of line, X coordinate.
  */
 private double xStart;

 /**
  * Start of line, Y coordinate.
  */
 private double yStart;

 /**
  * End of line, X coordinate.
  */
 private double xEnd;

 /**
  * End of line, Y coordinate.
  */
 private double yEnd;

 // CLASS METHODS:

 // (none)

 // CONSTRUCTOR(S):

 // (none)

 // INSTANCE METHODS:

 /**
  * Sets the end points of the line to the specified values.
  * 
  * @param xs
  *            X coordinate of start of line
  * @param ys
  *            Y coordinate of start of line
  * @param xe
  *            X coordinate of end of line
  * @param ye
  *            Y coordinate of end of line
  */

 public void setPoints( double xs, double ys, double xe, double ye )
 {
  this.xStart = xs;
  this.yStart = ys;
  this.xEnd = xe;
  this.yEnd = ye;
 }

 /**
  * Returns the length of the line.
  * 
  * @return A double value that is the Cartesian line length.
  */
 public double length( )
 {
  // The usual Pythagorean length formula

  return Math.sqrt( Math.pow( this.xEnd - this.xStart, 2 )
    + Math.pow( this.yEnd - this.yStart, 2 ) );
 }

 /**
  * "Slide" the line by moving it distance tx in the x direction and ty in
  * the y direction.
  * <p>
  * Note that tx or ty could be positive, negative, or zero.
  * 
  * @param tx
  *            Distance to translate in X direction
  * @param ty
  *            Distance to translate in Y direction
  */
 public void translate( double tx, double ty )
 {
  // Move x coordinates of start and end points

  this.xStart = this.xStart + tx;
  this.xEnd = this.xEnd + tx;

  // Move y coordinates of start and end points

  this.yStart = this.yStart + ty;
  this.yEnd = this.yEnd + ty;
 }

 /**
  * Find the slope of the line.
  * <p>
  * This implementation is overly particular about vertical lines and their
  * "direction".
  * 
  * @return The slope of the line, or negative or positive infinity as a
  *         double valuue.
  */
 public double slope( )
 {
  double result;

  if ( this.xEnd - this.xStart == 0.0 )
  {
   // The following avoids the actual division by 0
   // although apparently it doesn't much matter.

   if ( this.yEnd - this.yStart >= 0.0 )
   {
    result = Double.POSITIVE_INFINITY;
   }
   else
   {
    result = Double.NEGATIVE_INFINITY;
   }
  }
  else
  {
   result = ( this.yEnd - this.yStart ) / ( this.xEnd - this.xStart );
  }
  return result;
 }

 /**
  * Display information about the line on the screen.
  */
 public void printLineInfo( )
 {
  System.out.print( "Line from (" + this.xStart );
  System.out.print( " , " + this.yStart + ") to (" );
  System.out.println( this.xEnd + ", " + this.yEnd + ")" );
 }
 
 public String toString()
 { String result;
   
   result = "Line from (" + this.xStart;
   result = result + " , " + this.yStart + ") to (" ;
   result = result + this.xEnd + ", " + this.yEnd + ")" ;
  
   return result;
 }
}
